/*
 * @(#) $Header$
 *
 * Copyright (C)  2008  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package ca.forklabs.baselib.util.predicate;

import ca.forklabs.baselib.util.UnaryPredicate;
import ca.forklabs.baselib.util.predicate.Resources;

/**
 * Class {@code UnaryNegate} is a predicate adapter returning the opposite
 * of its adapted predicate.
 *
 * @param   <A>   the type of the argument object.
 *
 * @author <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.predicate.UnaryNegate">Daniel Léonard</a>
 * @version $Revision$
 */
public class UnaryNegate<A> implements UnaryPredicate<A> {

//---------------------------
// Instance variable
//---------------------------

   /** The adapted predicate. */
   private UnaryPredicate<A> predicate;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    * @param   predicate   the predicate to adapt.
    * @exception   NullPointerException   if the parameter is <code>null</code>.
    */
   public UnaryNegate(UnaryPredicate<A> predicate) {
      this.setPredicate(predicate);
      }


//---------------------------
// Accessor and mutator
//---------------------------

   /**
    * Changes the adapted predicate.
    * @param   predicate   the new predicate to adapt.
    * @exception   NullPointerException   if the parameter is {@code nul}.
    */
   @SuppressWarnings("nls")
   protected void setPredicate(UnaryPredicate<A> predicate) {
      if (null == predicate) {
         String message = this.getNullPredicateErrorMessage("f(x)");
         throw new NullPointerException(message);
         }
      this.predicate = predicate;
      }

   /**
    * Returns the adapted predicate.
    * @return   the adapted predicate.
    */
   protected UnaryPredicate<A> getPredicate() {
      return this.predicate;
      }


//---------------------------
// Implemented method from fork.util.UnaryPredicate
//---------------------------

   /**
    * Lets the adapted predicate evaluate the object and return the opposite of
    * the decision.
    * @param   a   the object to evaluate.
    * @return   {@code true} if the adapted predicate evaluates to
    *           {@code false}, {@code false} if the adapted predicate
    *           evaluates to {@code true}.
    */
   @Override
   @SuppressWarnings("hiding")
   public boolean eval(A a) {
      UnaryPredicate<A> predicate = this.getPredicate();

      boolean value = predicate.eval(a);
      value = !value;

      return value;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the error message saying that a predicate is {@code null}.
    * @param   predicate   the null predicate.
    * @return   the error message.
    */
   @SuppressWarnings("hiding")
   protected String getNullPredicateErrorMessage(String predicate) {
      String key = Resources.NULL_PREDICATE;
      String message = Resources.getLocalizedString(key, predicate);
      return message;
      }

   }
